package dao;

import utils.DBHelper;

import java.util.List;
import java.util.Map;

public class CartDao {

    // 查询购物车数据
    public List<Map<String, Object>> query(String uid){
        String sql = "SELECT\n" +
                "	p.product,\n" +
                "	p.image,\n" +
                "	p.price,\n" +
                "	c.amount,\n" +
                "	9 'limit'\n" +
                "FROM\n" +
                "	users u,\n" +
                "	(select * from cart where uid = ?) c,\n" +
                "	plist p\n" +
                "WHERE\n" +
                "	c.uid = u.id\n" +
                "AND c.pid = p.id";
        return DBHelper.selectListMap(sql, uid);
    }

    // 添加购物车数据 同时要将商品表里的数据减少 如果删除购物车里的数据 商品表里的要增加
    // 支付后生成订单 还要生成订单明细
    public void create(String product, String uid, String amount){
        String sql = "insert into cart (pid, uid, amount) select id, ?, ? from plist where product=?";
        DBHelper.update(sql, uid, amount, product);
    }
    public int exists(String product, String uid){
        String sql = "select pid from cart where uid=? and pid=(select id from plist where product=?)";
        Map<String, Object> list = DBHelper.selectOneMap(sql, uid, product);
        System.out.println(list);
        if (list == null){
            System.out.println(list);
            return 0; // 0表示购物车中没有该商品
        }
        return 1;
    }
    public int modify(String product, String amount){
        String sql = "update plist set count=count-? where product=? and count>0";
        return DBHelper.update(sql,amount, product);
    }
    // 修改购物车
    public int modifyCart(String uid, String product, String amount){
        String sql = " update cart set amount=amount+? where uid=? and pid = (select id from plist where product=?) ";
        return DBHelper.update(sql, amount, uid, product);
    }
    // 在商品界面修改购物车中商品数量
    public int modifyShop(String uid, String product, String amount){
        String sql = " update cart set amount=? where uid=? and pid = (select id from plist where product=?) ";
        return DBHelper.update(sql, amount, uid, product);
    }
    public void createOrder(String uid, String amount, String address, String method,String telphone){
        String sql = "INSERT INTO `order` (\n" +
                "	uid,\n" +
                "	amount,\n" +
                "	date,\n" +
                "	aid,\n" +
                "	`status`,\n" +
                "	method\n" +
                ") SELECT\n" +
                "	? uid,\n" +
                "	? amount,\n" +
                "	curdate() date,\n" +
                "	id aid,\n" +
                "	1 `status`,\n" +
                "	? method\n" +
                "FROM\n" +
                "	address\n" +
                "WHERE\n" +
                "	uid = ?\n" +
                "AND site = ? and telphone = ? ";
        DBHelper.update(sql, uid, amount,method, uid, address,telphone);
    }
    public void createItem(String uid, String product, String amount){
        String sql="INSERT INTO orderitem (oid, pid, price, count) SELECT\n" +
                "	(\n" +
                "		SELECT\n" +
                "			id\n" +
                "		FROM\n" +
                "			`order`\n" +
                "		WHERE\n" +
                "			uid = ?\n" +
                "		ORDER BY\n" +
                "			date DESC\n" +
                "		LIMIT 1\n" +
                "	) oid,\n" +
                "	id pid,\n" +
                "	price,\n" +
                "	? count\n" +
                "FROM\n" +
                "	plist\n" +
                "WHERE\n" +
                "	product = ?";
        DBHelper.update(sql, uid, amount, product);
    }
    public void removeCart(String uid, String product){
        String sql = "delete from cart where uid=? and pid=(select id from plist where product=?)";
        DBHelper.update(sql, uid, product);
    }
    public int queryAddress(String uid, String name, String telphone, String address){
        String sql = "select * from address where name=? and telphone=? and uid=? and site=?";
        Map<String, Object> list = DBHelper.selectOneMap(sql, name, telphone, uid, address);
        if (list != null){
            // 不为空 则存在
            return 1;
        }
        return 0;
    }

    public void createAddress(String uid, String name, String telphone, String address){
        String sql = "insert into address (name, telphone, uid, site) values (?, ?, ?, ?)";
        DBHelper.update(sql, name, telphone, uid, address);
    }

    // 在购物车中查找商品数量
    public String queryProduct(String uid, String product, String amount){
        String sql = "select amount from cart c, plist p where c.uid = ? and p.id = c.pid and p.product = ? and amount = ?";
        Map<String, Object> map = DBHelper.selectOneMap(sql, uid, product, amount);
        return String.valueOf(map==null ? 0 : map.get("amount"));
    }

    // 删除购物车中的商品
    public void remove(String uid, String product, String amount){
        String sql = "delete from cart where uid = ? and pid = (select id from plist where product = ?)";
        DBHelper.update(sql, uid, product);
        modify(product, "-" + amount);
    }


}
